﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace sunsetmp
{
    /// <summary>
    /// Interaction logic for MediaWindow.xaml
    /// </summary>
    public partial class MediaWindow : Window
    {
        bool fullscreen = false;
        Core core;

        [DllImport("user32.dll")]
        static extern uint GetDoubleClickTime(); //Возвращает "время" двойного щелчка
        System.Timers.Timer timeClick = new System.Timers.Timer((int)GetDoubleClickTime()) { AutoReset = false };

        public MediaWindow(Core c)
        {
            InitializeComponent();
            VideoElement.Stretch = Stretch.Uniform;
            core = c;
        }

        private void Grid_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        //сворачивание/разворачивание при двойном клике
        private void VidMedia_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!timeClick.Enabled)
            {
                timeClick.Enabled = true;
                return;
            }

            if (timeClick.Enabled)
            {
                if (fullscreen)
                {
                    this.WindowState = WindowState.Normal;
                    this.Topmost = false;
                    fullscreen = false;
                }
                else
                {
                    this.WindowState = System.Windows.WindowState.Maximized;
                    this.Topmost = true;
                    fullscreen = true;
                }
            }
        }

        //локальные горячие клавиши
        private void Window_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
                if (fullscreen)
                {
                    this.WindowState = WindowState.Normal;
                    this.Topmost = false;
                    fullscreen = false;
                }
            if (e.Key == Key.Enter)
                if (fullscreen)
                {
                    this.WindowState = WindowState.Normal;
                    this.Topmost = false;
                    fullscreen = false;
                }
                else
                {
                    this.WindowState = System.Windows.WindowState.Maximized;
                    this.Topmost = true;
                    fullscreen = true;
                }
            if (e.Key == Key.Space)
            {
                core.PlayPause();
            }
        }

        private void Window_Closing_1(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void PlayIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            core.PlayPause();
        }

        private void StopIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            core.Stop();
        }

        private void PrevIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            core.PrevTrack();
        }

        private void NextIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            core.NextTrack();
        }

        private void PauseIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            core.PlayPause();
        }
    }
}
